// import Shop from '../../model/shop'
import DlyAdminMaterial from '../../model/dlyadminmaterial'
import ApiError from '../../error/ApiError'
import ApiErrorNames from '../../error/ApiErrorNames'
class MaterialController {
	static async add(ctx) {
		// console.log('ctx', ctx.request.header.jwtuserinfo)
		console.log('ctx', ctx.request.body)
		const list = ctx.request.body || {}
		/**
		 * list[] path: string, type:1|2 
		 */
		console.log('list', list)
		if(!list || !list.length || !Array.isArray(list)) {
			throw new ApiError(ApiErrorNames.MateriallistNAN)
		}
		let addkeys = true
		list.forEach(item => {
		    if(!item.type ) {
				addkeys = false
				throw new ApiError(ApiErrorNames.MateriallisType)
			}
			if(!(item.type === 1 || item.type === 2)) {
				addkeys = false

				throw new ApiError(ApiErrorNames.MateriallisType)
			}
			// if(item.type )
			if(!item.path) {
				addkeys = false

			    throw new ApiError(ApiErrorNames.MateriallistPath)
			}
		})
		if(addkeys) {
			// 保存
			const addWei = await DlyAdminMaterial.insertMany(list)
			ctx.body = addWei
		}
		
	}

	static async get(ctx) {
		ctx.verifyParams({
			type: { type: 'string', required: true },
			// uuId: { type: 'string', required: true },
		})
		const { page=1, pageSize=20 , type} = ctx.query

		const list = await DlyAdminMaterial.find({type}).skip((page-1)*pageSize).limit(pageSize*1)
		// 获取总数
		const total = await DlyAdminMaterial.countDocuments({type})
		ctx.body = {list, total}
	    // const {page, pageSize} = ctx.q

	}
	static async del(ctx) {
	    if (!ctx.params || !ctx.params.id) {
			throw new ApiError(ApiErrorNames.NoParamsData)
		}

		const {id} = ctx.params

		const del = await DlyAdminMaterial.deleteOne({_id: id})

		ctx.body = del
	}
}

export default MaterialController
